/**
 * @file album.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"
#include "lvgl/src/lv_draw/lv_draw_img_jpeg_decoder.h"

#if USE_LV_WATCH_ALBUM != 0

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

extern lv_obj_t * voice_msg_create_send(lv_obj_t * activity_obj);
/**********************
 *  STATIC PROTOTYPES
 **********************/
static lv_obj_t * album_photo_bg_create(lv_obj_t * par);
static lv_obj_t * album_setting_create(lv_obj_t * activity_obj);
static void album_photo_item_create(lv_obj_t * par);
static void album_setting_order_create(lv_obj_t * par);
static void album_setting_delete_create(lv_obj_t * par, uint8_t del_one);
static void album_setting_event_cb(lv_obj_t * obj, lv_event_t e);
static void album_back_event_cb(lv_obj_t * obj, lv_event_t e);
static lv_res_t album_img_signal(lv_obj_t * obj, lv_signal_t sign, void * param);
static void album_share_event_cb(lv_obj_t * obj, lv_event_t e);
static void album_delete_event_cb(lv_obj_t * obj, lv_event_t e);
static void album_opt_delete_event_cb(lv_obj_t * obj, lv_event_t e);
static void album_setting_order_event_cb(lv_obj_t * obj, lv_event_t e);
static void album_setting_opt_event_cb(lv_obj_t * obj, lv_event_t e);
static lv_res_t album_item_page_scrl_signal(lv_obj_t * page_scrl, lv_signal_t sign, void * param);
static void album_cancel_btn_event_cb(lv_obj_t * btn, lv_event_t e);
static void album_ok_btn_event_cb(lv_obj_t * btn, lv_event_t e);
static void album_get_small_photo(void);
static album_photo_info_t * album_get_del_img_node(lv_obj_t * img, uint8_t type);
static void album_set_photo_order_label_text(void);
static void album_view_photo_tabview_create(uint32_t img_index);
static void album_view_photo_opt_create(lv_obj_t * img_view);
static void album_prepare_destory(lv_obj_t * activity_obj);
static void album_reset_photo_ll_selected(void);
static void album_set_photo_ll_selected(uint8_t num, uint8_t sel);
static void album_photo_share_confirm_create(lv_obj_t * par, voice_msg_contact_t * contact, void * src);
static void album_share_cancel_btn_event_cb(lv_obj_t * btn, lv_event_t e);
static void album_share_ok_btn_event_cb(lv_obj_t * btn, lv_event_t e);
static void album_get_size_from_path(char * path, uint32_t * size);
static void album_update_photo_file(char * path);
static void album_free_prewview_src(void);
static void album_reset_node_index(void);
static void album_recreate_preview(void);
#if USE_LV_BLUETOOTH != 0
static void album_share_to_voice_msg_action(lv_obj_t * btn, lv_event_t e);
static void album_share_to_bt_action(lv_obj_t * btn, lv_event_t e);
static void album_share_select_dev_action(lv_obj_t * btn, lv_event_t e);
#endif

/**********************
 *  STATIC VARIABLES
 **********************/
static lv_ll_t * photo_ll = NULL;
static lv_signal_cb_t orignal_page_scrl_signal;
static lv_signal_cb_t orignal_img_signal;
/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
lv_obj_t * album_create(lv_obj_t * activity_obj)
{
    lv_obj_t * bg;
    lv_obj_t * obj;

    if(NULL == activity_obj) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_ALBUM;
        activity_ext.create = album_create;
        activity_ext.prepare_destory =  album_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(NULL == activity_obj) {
            return NULL;
        }
    }

    obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(NULL == obj) {
        return NULL;
    }

    /*Allocate the object type specific extended data*/
    lv_album_obj_ext_t * ext = lv_obj_allocate_ext_attr(obj, sizeof(lv_album_obj_ext_t));
    LV_ASSERT_MEM(ext);
    if(ext == NULL) return NULL;
    ext->time_order = 0;
    ext->photo_cnt = album_photo_ll_get_nodecnt();
    ext->preview_mode = 0;
    ext->obj_par = obj;
    ext->selected_cnt = 0;
    ext->from_camera = 0;
    ext->pressing = false;
    ext->del_bg = NULL;
    ext->share_bg = NULL;
    ext->timer = NULL;

    bg = album_photo_bg_create(obj);

    if(ext->photo_cnt > 0) {
        album_get_small_photo();
        album_reset_node_index();
        album_photo_item_create(bg);
    }

    return obj;
}

lv_album_obj_ext_t * album_get_ext(void)
{
    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_ALBUM);
    if(activity_obj == NULL) return NULL;

    lv_obj_t * watch_obj;
    lv_watch_get_child_obj(activity_obj, lv_watch_obj_signal, &watch_obj);
    if(watch_obj == NULL) return NULL;

    if(watch_obj) {
        lv_album_obj_ext_t * ext = lv_obj_get_ext_attr(watch_obj);
        return ext;
    }
    return NULL;
}

void album_create_event_cb(lv_obj_t * btn, lv_event_t e)
{
    (void)btn;
    if(LV_EVENT_CLICKED == e)
    {
        //lv_jpeg_decoder_init();
        lv_watch_png_cache_all_free();
        album_photo_ll_init();
        album_load_photo_path();
        lv_obj_t * obj = album_create(NULL);
        LV_ASSERT_MEM(obj);
    }
}

lv_res_t album_create_from_camera(void)
{
    lv_obj_t * obj = album_create(NULL);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) return LV_RES_OK;

    lv_album_obj_ext_t * ext = album_get_ext();
    ext->from_camera = 1;
    ext->timer = NULL;
    if(ext->photo_cnt > 0)
        album_view_photo_tabview_create(0);
    return LV_RES_OK;
}

void album_photo_ll_init(void)
{
    if(photo_ll == NULL) {
        photo_ll = lv_mem_alloc(sizeof(lv_ll_t));
        memset(photo_ll, 0, sizeof(lv_ll_t));
        _lv_ll_init(photo_ll, sizeof(album_photo_info_t));
    }
    album_photo_path_init();
}

const lv_ll_t * album_get_photo_ll(void)
{
    return photo_ll;
}

void album_photo_ll_destroy(void)
{
    if(photo_ll) {
        _lv_ll_clear(photo_ll);
        lv_mem_free(photo_ll);
        photo_ll = NULL;
    }
}

uint8_t album_photo_ll_get_nodecnt(void)
{
    uint8_t cnt = 0;
    lv_ll_node_t * node = NULL;
    node = _lv_ll_get_head(photo_ll);
    while(node) {
        cnt++;
        node = _lv_ll_get_next(photo_ll, node);
    }
    printf("album photo cnt = %d\n", cnt);
    return cnt;
}

#if USE_LV_BLUETOOTH != 0
void album_select_share_way(lv_obj_t * par)
{
    lv_obj_t * bg = lv_cont_create(par, NULL);
    lv_obj_set_size(bg, LV_HOR_RES, LV_VER_RES);
    lv_obj_set_style_local_bg_color(bg, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xB6, 0xF7, 0xFF));

    lv_obj_t * img_back = lv_img_create(bg, NULL);
    lv_img_set_src(img_back, ICON_ALBUM_BACK_BLUE);
    if(lv_obj_get_base_dir(par) == LV_BIDI_DIR_RTL) {
        lv_obj_align(img_back, NULL, LV_ALIGN_IN_TOP_RIGHT, 0, 0);
        lv_img_set_angle(img_back, 1800);
    } else {
        lv_obj_align(img_back, NULL, LV_ALIGN_IN_TOP_LEFT, 0, 0);
    }
    lv_obj_set_click(img_back, true);
    lv_watch_set_free_num(img_back, ALBUM_BACK_SEL_SHARE);
    lv_obj_set_event_cb(img_back, album_back_event_cb);

    lv_obj_t * label = lv_label_create(bg, NULL);
    lv_label_set_text_id(label, WATCH_TEXT_ID_SHARE_TO);
    lv_obj_align(label, NULL, LV_ALIGN_IN_TOP_MID, 0, 15);
    lv_obj_set_style_local_text_color(label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLUE);

    lv_obj_t * btn_label;
    lv_obj_t * vm_btn = lv_btn_create(bg, NULL);
    lv_obj_set_size(vm_btn, LV_HOR_RES, LV_VER_RES / 8);
    lv_obj_align(vm_btn, label, LV_ALIGN_OUT_BOTTOM_MID, 0, 15);
    lv_theme_apply(vm_btn, LV_THEME_LIST_BTN);
    lv_obj_set_style_local_bg_opa(vm_btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_radius(vm_btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_border_width(vm_btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 1);
    lv_obj_set_style_local_border_side(vm_btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_BORDER_SIDE_BOTTOM);
    lv_obj_set_style_local_border_width(vm_btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, 1);
    lv_obj_set_style_local_border_side(vm_btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, LV_BORDER_SIDE_BOTTOM);
    lv_obj_set_style_local_pad_left(vm_btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 10);
    lv_obj_set_style_local_pad_right(vm_btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 10);
    lv_obj_set_event_cb(vm_btn, album_share_to_voice_msg_action);
    lv_btn_set_layout(vm_btn, LV_LAYOUT_ROW_MID);
    btn_label = lv_label_create(vm_btn, NULL);
    lv_obj_add_style(btn_label, LV_LABEL_PART_MAIN, &lv_watch_font20_black);
    lv_label_set_text_id(btn_label, WATCH_TEXT_ID_WECHAT);
    lv_obj_t * bt_btn = lv_btn_create(bg, vm_btn);
    lv_obj_align(bt_btn, vm_btn, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
    lv_obj_set_event_cb(bt_btn, album_share_to_bt_action);
    btn_label = lv_label_create(bt_btn, btn_label);
    lv_label_set_text_id(btn_label, WATCH_TEXT_ID_BLUETOOTH);

    lv_obj_t * album_act_obj = lv_watch_get_activity_obj(ACT_ID_ALBUM);
    lv_obj_t * camera_act_obj = lv_watch_get_activity_obj(ACT_ID_CAMERA);
    if(album_act_obj == NULL && camera_act_obj) {
        lv_obj_t * watch_obj = NULL;
        lv_camera_obj_ext_t  * ext = NULL;
        lv_watch_get_child_obj(camera_act_obj, lv_watch_obj_signal, &watch_obj);
        if(watch_obj)
            ext = lv_obj_get_ext_attr(watch_obj);
        lv_img_dsc_t * src = (lv_img_dsc_t *)lv_img_get_src(ext->img);
        lv_fs_file_t file;
        lv_fs_res_t res = lv_fs_open(&file, PHOTO_SHARE_TMEP_PATH, LV_FS_MODE_WR);
        if(res == LV_FS_RES_OK) {
            uint32_t rn;
            res = lv_fs_write(&file, (void *)src->data, src->data_size, &rn);
            if(res != LV_FS_RES_OK) {
                printf("write temp bt share file error,errno = %d\n", res);
            } else {
                printf("write temp bt share file OK rn = %d\n", rn);
                lv_watch_set_free_ptr(bt_btn, (void*)PHOTO_SHARE_TMEP_PATH);
            }
            lv_fs_close(&file);
        }
    } else if(album_act_obj) {
        lv_obj_set_parent(bg, album_act_obj);
        lv_album_obj_ext_t * ext = album_get_ext();
        ext->share_bg = bg;
        album_photo_info_t * node = NULL;
        node = (album_photo_info_t *)_lv_ll_get_head(photo_ll);
        while(node) {
            if(node->selected == 1) {
                lv_watch_set_free_ptr(bt_btn, (void *)node);
                break;
            }
            node = (album_photo_info_t *)_lv_ll_get_next(photo_ll, node);
        }
    }
}

void album_send_file_finish_action(void)
{
    lv_album_obj_ext_t * ext = album_get_ext();
    if(NULL == ext)
        return;
    if(ext->share_bg) {
        lv_obj_del(ext->share_bg);
        ext->share_bg = NULL;
        album_recreate_preview();
        album_reset_photo_ll_selected();
    }
}

#endif

void album_photo_share_event_cb(lv_obj_t * img, lv_event_t e)
{
    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_VOICE_MSG);
    if(NULL == activity_obj) return;
    lv_obj_t * obj = NULL;
    lv_watch_get_child_obj(activity_obj, lv_watch_obj_signal, &obj);
    lv_voice_msg_ext_t  * ext = lv_obj_get_ext_attr(obj);

    if(LV_EVENT_PRESSED == e) {
        ext->pressing = true;
    } else if((true == ext->pressing) && (LV_EVENT_CLICKED == e)) {
        ext->pressing = false;
        /*click the contact*/
        lv_obj_t * img_portrait = lv_obj_get_child_back(img, NULL);
        album_photo_share_confirm_create(obj, (voice_msg_contact_t *)lv_obj_get_user_data(img_portrait).user_data, (void *)lv_img_get_src(img_portrait));
    }
}

void album_photo_share_flag_reset(void)
{
    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_ALBUM);
    if(activity_obj) {
        album_photo_info_t * node = NULL;
        node = (album_photo_info_t *)_lv_ll_get_head(photo_ll);
        while(node) {
            if(node->selected == 1) {
                node->selected = 0;
                break;
            }
            node = (album_photo_info_t *)_lv_ll_get_next(photo_ll, node);
        }
    }
}

void album_photo_path_init(void)
{
    lv_fs_file_t path_file;
    lv_fs_res_t res;
    res = lv_fs_open(&path_file, PHOTO_PATH_FILE, LV_FS_MODE_RD);
    if(res == LV_FS_RES_OK) {
        lv_fs_close(&path_file);
    } else {
        res = lv_fs_open(&path_file, PHOTO_PATH_FILE, LV_FS_MODE_WR);
        if(res == LV_FS_RES_OK) {
            lv_fs_close(&path_file);
        } else {
            printf("create file failed\n");
        }
    }
}

void album_load_photo_path(void)
{
    lv_fs_file_t file;
    char * temp_buf = NULL;
    lv_fs_res_t res = lv_fs_open(&file, PHOTO_PATH_FILE, LV_FS_MODE_RD);
    if(res == LV_FS_RES_OK) {
        uint32_t rn;
        uint32_t size;
        lv_fs_size(&file, &size);
        temp_buf = lv_mem_alloc(size + 1);
        memset(temp_buf, 0, size + 1);
        lv_fs_read(&file, temp_buf, size, &rn);
        uint8_t file_cnt = strlen(temp_buf) / PHOTO_PATH_LEN;
        lv_fs_close(&file);

        uint8_t i = 0;
        album_photo_info_t * node;

        for(i = 0; i < file_cnt; i++) {
            node = (album_photo_info_t *)_lv_ll_ins_tail(photo_ll);
            memset(node->file_path, 0, PHOTO_PATH_LEN + 1);
            strncpy(node->file_path, &temp_buf[PHOTO_PATH_LEN * i], PHOTO_PATH_LEN);
            node->file_path[PHOTO_PATH_LEN] = '\0';
            album_get_size_from_path(node->file_path, &node->size);
            printf("album_load_photo_path size = %d\n", node->size);
            node->selected = 0;
            node->index = i;
            node->preview_src = NULL;
        }
        lv_mem_free(temp_buf);
    } else
        printf("album_load_photo_path open photo_path file failed\n");
}

lv_fs_res_t album_photo_path_write(char * str)
{
    lv_fs_file_t path_file;
    lv_fs_res_t res = lv_fs_open(&path_file, PHOTO_PATH_FILE, (LV_FS_MODE_RD | LV_FS_MODE_APPEND));
    if(res == LV_FS_RES_OK) {
        uint32_t rn;
        uint32_t size;
        uint8_t cnt = album_photo_ll_get_nodecnt();
        lv_fs_size(&path_file, &size);
        if(cnt > 1) {
            if((cnt - 1) * PHOTO_PATH_LEN > size) {
                printf("seek path file error: cnt:%d, file size:%d\n", cnt, size);
                lv_fs_close(&path_file);
                return LV_FS_RES_UNKNOWN;
            }
            lv_fs_seek(&path_file, (cnt - 1) * PHOTO_PATH_LEN);
        }
        res = lv_fs_write(&path_file, (void *) str, PHOTO_PATH_LEN + 1, &rn);
        if(res != LV_FS_RES_OK)
            printf("write path file error res = %d\n", res);
        lv_fs_close(&path_file);
    } else {
        printf("open path file error res = %d\n", res);
    }

    return res;
}

void album_destory(bool start_cam)
{
    lv_album_obj_ext_t * ext = album_get_ext();
    if(ext->timer)
    {
        Hal_Timer_Stop(ext->timer);
        ext->timer = NULL;
    }
    album_free_prewview_src();
    if(ext->from_camera == 0 || start_cam == false) {
        album_photo_ll_destroy();
    } else {
        camera_start_from_album();
    }
    lv_watch_png_cache_all_free();
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static void album_prepare_destory(lv_obj_t * activity_obj)
{
    album_destory(true);
}

static void album_setting_prepare_destory(lv_obj_t * activity_obj)
{
    lv_watch_png_cache_all_free();
    lv_album_obj_ext_t * ext = album_get_ext();
    if(ext->del_bg) {
        uint8_t img_num = lv_obj_get_user_data(lv_obj_get_child_back(ext->del_bg, NULL)).user_num;
        album_photo_info_t * node = NULL;
        node = (album_photo_info_t *)_lv_ll_get_head(photo_ll);
        while(node) {
            if(node->index == img_num) {
                lv_img_cache_invalidate_src(node->preview_src);
                node->preview_src->header.w = 72;
                node->preview_src->header.h = 72;
                break;
            }
            node = (album_photo_info_t *)_lv_ll_get_next(photo_ll, node);
        }
        lv_obj_del(ext->del_bg);
        ext->del_bg = NULL;
    }
}

static void album_free_prewview_src(void)
{
    album_photo_info_t * node = NULL;
    node = (album_photo_info_t *)_lv_ll_get_head(photo_ll);
    while(node) {
        if(node->preview_src) {
            lv_img_cache_invalidate_src(node->preview_src);
            lv_mem_free(node->preview_src);
            node->preview_src = NULL;
        }
        node = (album_photo_info_t *)_lv_ll_get_next(photo_ll, node);
    }
}

static void album_get_size_from_path(char * path, uint32_t * size)
{
    lv_fs_file_t file;
    lv_fs_res_t res = lv_fs_open(&file, path, LV_FS_MODE_RD);
    if(res == LV_FS_RES_OK) {
        lv_fs_size(&file, size);
        lv_fs_close(&file);
    }
    else {
        printf("%s:open file failed res = %d\n", __FUNCTION__, res);
    }
}

static void album_update_photo_file(char * path)
{
    lv_fs_file_t file;
    char * temp_buf = NULL;
    char * null_buf = NULL;
    char del_buf[PHOTO_PATH_LEN + 1];
    uint8_t i = 0;
    lv_album_obj_ext_t * ext = album_get_ext();
    ext->photo_cnt = album_photo_ll_get_nodecnt();
    lv_fs_res_t res = lv_fs_open(&file, PHOTO_PATH_FILE, (LV_FS_MODE_RD | LV_FS_MODE_APPEND));
    if(res == LV_FS_RES_OK) {
        uint32_t rn;
        while(i < ext->photo_cnt) {
            if(i > 0)
                lv_fs_seek(&file, i * PHOTO_PATH_LEN);
            memset(del_buf, 0, PHOTO_PATH_LEN + 1);
            lv_fs_read(&file, del_buf, PHOTO_PATH_LEN, &rn);
            if(0 == strncmp(path, del_buf, PHOTO_PATH_LEN)) {
                printf("album_update_photo_file del_path=%s\n", del_buf);
                uint32_t file_size;
                uint32_t buf_size;
                lv_fs_size(&file, &file_size);
                buf_size = file_size - PHOTO_PATH_LEN * (i + 1);
                temp_buf = lv_mem_alloc(buf_size);
                memset(temp_buf, 0, buf_size);
                lv_fs_seek(&file, (i + 1) * PHOTO_PATH_LEN);
                lv_fs_read(&file, temp_buf, buf_size, &rn);

                lv_fs_seek(&file, i * PHOTO_PATH_LEN);
                null_buf = lv_mem_alloc(file_size - i * PHOTO_PATH_LEN);
                memset(null_buf, 0, file_size - i * PHOTO_PATH_LEN);
                lv_fs_write(&file, null_buf, file_size - i * PHOTO_PATH_LEN, &rn);
                lv_mem_free(null_buf);

                lv_fs_seek(&file, i * PHOTO_PATH_LEN);
                lv_fs_write(&file, temp_buf, buf_size, &rn);
                lv_mem_free(temp_buf);
                break;
            }
            i++;
        }
        if(i == ext->photo_cnt) {
            printf("album_update_photo_file cannot find match path\n");
        }
        lv_fs_close(&file);
    } else {
        printf("album_update_photo_file open file failed res = %d\n", res);
    }
}

static void album_reset_node_index(void)
{
    uint8_t i = 0;
    lv_album_obj_ext_t * ext = album_get_ext();
    album_photo_info_t * node = NULL;
    if(ext->time_order)
        node = (album_photo_info_t *)_lv_ll_get_head(photo_ll);
    else
        node = (album_photo_info_t *)_lv_ll_get_tail(photo_ll);
    while(node) {
        node->index = i;
        if(ext->time_order)
            node = (album_photo_info_t *)_lv_ll_get_next(photo_ll, node);
        else
            node = (album_photo_info_t *)_lv_ll_get_prev(photo_ll, node);
        i++;
    }
}

static void album_get_small_photo()
{
    lv_album_obj_ext_t * ext = album_get_ext();
    album_photo_info_t * node = NULL;
    if(ext->time_order)
        node = (album_photo_info_t *)_lv_ll_get_head(photo_ll);
    else
        node = (album_photo_info_t *)_lv_ll_get_tail(photo_ll);
    while(node) {
        node->preview_src = lv_mem_alloc(sizeof(lv_img_dsc_t));
        memset(node->preview_src, 0, sizeof(lv_img_dsc_t));
        node->preview_src->header.always_zero = 0;
        node->preview_src->data_size = node->size;
        node->preview_src->header.cf = LV_IMG_CF_RAW;//need add ext .jpg for the file
        node->preview_src->header.reserved = 1;
        node->preview_src->data = (const uint8_t *)node->file_path;
        node->preview_src->header.w = 72;
        node->preview_src->header.h = 72;
        if(ext->time_order)
            node = (album_photo_info_t *)_lv_ll_get_next(photo_ll, node);
        else
            node = (album_photo_info_t *)_lv_ll_get_prev(photo_ll, node);
    }
}

static album_photo_info_t * album_get_del_img_node(lv_obj_t * img, uint8_t type)
{
    uint8_t i = 0;
    album_photo_info_t * node = NULL;
    lv_album_obj_ext_t * ext = album_get_ext();
    lv_ll_t * list = (lv_ll_t *)album_get_photo_ll();
    if(type == 1) {//del one
        node = (album_photo_info_t *)_lv_ll_get_head(list);
        for(i = 0; i < ext->photo_cnt; i++) {
            if(node->index ==  lv_obj_get_user_data(img).user_num) {
                node->selected = 1;
                return node ;
            }
            node = (album_photo_info_t *)_lv_ll_get_next(list, node);
        }
    } else if(type == 3) { // del all
        if(ext->time_order)
            node = (album_photo_info_t *)_lv_ll_get_head(list);
        else
            node = (album_photo_info_t *)_lv_ll_get_tail(list);

        return node;
    } else if(type == 2) {//del muti
        if(ext->time_order)
            node = (album_photo_info_t *)_lv_ll_get_tail(list);
        else
            node = (album_photo_info_t *)_lv_ll_get_head(list);

        for(i = 0; i < ext->photo_cnt; i++) {
            if(node->selected == 1) {
                return node;
            }
            if(ext->time_order) {
                node = (album_photo_info_t *)_lv_ll_get_prev(list, node);
            } else {
                node = (album_photo_info_t *)_lv_ll_get_next(list, node);
            }
        }
    }
    return NULL;
}

static void album_photo_delete(uint8_t type)
{
    album_photo_info_t * node = NULL;
    album_photo_info_t * next_node = NULL;
    lv_album_obj_ext_t * ext = album_get_ext();
    lv_ll_t * list = (lv_ll_t *)album_get_photo_ll();
    node = (album_photo_info_t *)_lv_ll_get_head(list);

    /*change gui task to lower priority to keep audio playing smoothly*/
    #ifndef BUILD_IN_PC_SIMULATOR
    uint8_t old_prio;
    uos_chgpriority_task(gui_task_id, 250, &old_prio);
    #endif

    while(node) {
        //next node
        next_node = (album_photo_info_t *)_lv_ll_get_next(list, node);
        if(((type == 1 || type == 2) && node->selected == 1) || type == 3) {
            printf("album_photo_delete path = %s\n", node->file_path);
            lv_fs_remove(node->file_path);
            album_update_photo_file(node->file_path);
            lv_img_cache_invalidate_src(node->preview_src);
            lv_mem_free(node->preview_src);
            _lv_ll_remove(list, node);
            lv_mem_free(node);
            if(type == 1)
                break;
        }
        node = next_node;
    }
    ext->photo_cnt = album_photo_ll_get_nodecnt();
    album_reset_node_index();

    /*revert gui task priority */
    #ifndef BUILD_IN_PC_SIMULATOR
    uos_chgpriority_task(gui_task_id, old_prio, &old_prio);
    #endif
}

static void album_set_photo_ll_selected(uint8_t num, uint8_t sel)
{
    uint8_t i = 0;
    album_photo_info_t * node = NULL;
    lv_album_obj_ext_t * ext = album_get_ext();
    lv_ll_t * list = (lv_ll_t *)album_get_photo_ll();
    if(ext->time_order)
        node = (album_photo_info_t *)_lv_ll_get_head(list);
    else
        node = (album_photo_info_t *)_lv_ll_get_tail(list);
    for(i = 0; i < ext->photo_cnt; i++) {
        if(i == num)
            node->selected = sel;
        if(ext->time_order)
            node = (album_photo_info_t *)_lv_ll_get_next(list, node);
        else
            node = (album_photo_info_t *)_lv_ll_get_prev(list, node);
    }
}

static void album_reset_photo_ll_selected(void)
{
    uint8_t i = 0;
    album_photo_info_t * node = NULL;
    lv_album_obj_ext_t * ext = album_get_ext();
    lv_ll_t * list = (lv_ll_t *)album_get_photo_ll();

    node = (album_photo_info_t *)_lv_ll_get_head(list);
    for(i = 0; i < ext->photo_cnt; i++) {
        node->selected = 0;
        node = (album_photo_info_t *)_lv_ll_get_next(list, node);
    }
    ext->selected_cnt = 0;
}

static lv_obj_t * album_get_obj(void)
{
    lv_obj_t * watch_obj = NULL;

    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_ALBUM);
    if(activity_obj) {
        lv_watch_get_child_obj(activity_obj, lv_watch_obj_signal, &watch_obj);
    }
    return watch_obj;
}
static void album_set_photo_order_label_text()
{
    lv_album_obj_ext_t * ext = album_get_ext();
    if(ext == NULL || ext->orderopt_label == NULL)
        return;

    char opt[25];
    memset(opt, 0, 25);
    if(ext->time_order) {
        snprintf(opt, 25, "%s", (char *)lv_lang_get_text(WATCH_TEXT_ID_TIME_ORDER));
    } else {
        snprintf(opt, 25, "%s", (char *)lv_lang_get_text(WATCH_TEXT_ID_TIME_REVERSE_ORDER));
    }
    lv_label_set_text(ext->orderopt_label, opt);
}

static lv_obj_t * album_photo_bg_create(lv_obj_t * par)
{
    lv_obj_t * bg;
    lv_album_obj_ext_t * ext = album_get_ext();
    if(ext->photo_cnt == 0) {
        bg = lv_img_create(par, NULL);
        lv_img_set_src(bg, ICON_ALBUM_NO_PHOTO);

        lv_obj_t * label_nophoto = lv_label_create(bg, NULL);
        lv_label_set_text_id(label_nophoto, WATCH_TEXT_ID_NOPHOTO);
        lv_obj_align(label_nophoto, NULL, LV_ALIGN_IN_BOTTOM_MID, 10, -20);
        lv_obj_add_style(label_nophoto, LV_LABEL_PART_MAIN, &lv_watch_font20_black);
        lv_obj_set_click(label_nophoto, false);

    } else {
        bg = lv_cont_create(par, NULL);
        lv_obj_set_size(bg, LV_HOR_RES, LV_VER_RES);
        lv_obj_set_style_local_bg_color(bg, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xB6, 0xF7, 0xFF));
    }
    lv_obj_set_click(bg, false);

    lv_obj_t * cont = lv_cont_create(bg, NULL);
    lv_obj_set_size(cont, LV_HOR_RES, LV_VER_RES / 7);
    lv_obj_align(cont, bg, LV_ALIGN_IN_TOP_LEFT, 0, 0);
    lv_obj_set_style_local_bg_opa(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_TRANSP);
    lv_obj_set_style_local_pad_left(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_right(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_cont_set_layout(cont, LV_LAYOUT_ROW_MID);
    lv_obj_set_click(cont, false);


    lv_obj_t * img_back = lv_img_create(cont, NULL);
    lv_img_set_src(img_back, ICON_ALBUM_BACK_BLUE);
    if(lv_obj_get_base_dir(cont) == LV_BIDI_DIR_RTL)
        lv_img_set_angle(img_back, 1800);
    lv_watch_set_free_num(img_back, 2);
    lv_obj_set_click(img_back, true);
    lv_obj_set_event_cb(img_back, album_back_event_cb);
    lv_obj_set_adv_hittest(img_back, false);

    lv_obj_t * label_ph = lv_label_create(cont, NULL);
    lv_label_set_text_id(label_ph, WATCH_TEXT_ID_ALBUM);
    lv_obj_add_style(label_ph, LV_LABEL_PART_MAIN, &lv_watch_font20_black);
    lv_obj_set_click(label_ph, false);

    lv_obj_t * img_setting = lv_img_create(cont, NULL);
    lv_img_set_src(img_setting, ICON_ALBUM_MORE_SETTING);
    lv_obj_set_click(img_setting, true);
    lv_obj_set_adv_hittest(img_setting, false);
    ext->img_set = img_setting;
    if(ext->photo_cnt == 0)
        lv_obj_set_hidden(img_setting, true);

    uint8_t inner_pad = (LV_HOR_RES - lv_obj_get_width(img_back) - lv_obj_get_width(img_setting) - lv_obj_get_width(label_ph)) / 2;
    lv_obj_set_style_local_pad_inner(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, inner_pad);

    return bg;
}

static void album_photo_item_create(lv_obj_t * par)
{
    lv_album_obj_ext_t * ext = album_get_ext();
    uint8_t line = ext->photo_cnt % 3 == 0 ? ext->photo_cnt / 3 : ext->photo_cnt / 3 + 1;

    ext->img_page  = lv_page_create(par, NULL);
    lv_obj_set_size(ext->img_page, LV_HOR_RES, 200);
    lv_obj_align(ext->img_page, NULL, LV_ALIGN_IN_BOTTOM_MID, 0, 0);
    lv_obj_add_style(ext->img_page, LV_PAGE_PART_BG, &lv_watch_style_transp);
    lv_obj_add_style(ext->img_page, LV_PAGE_PART_SCROLLABLE, &lv_watch_style_transp);
    lv_obj_add_style(ext->img_page, LV_PAGE_PART_EDGE_FLASH, &lv_watch_cont_opa1);
    lv_watch_obj_add_element(ext->img_page);
    lv_obj_t * page_scrl = lv_page_get_scrl(ext->img_page);
    orignal_page_scrl_signal = lv_obj_get_signal_cb(page_scrl);
    lv_obj_set_signal_cb(page_scrl, album_item_page_scrl_signal);

    lv_obj_t * cont = lv_cont_create(ext->img_page, NULL);
    lv_obj_set_size(cont, LV_HOR_RES - 8, (72 + 3) * line);
    lv_obj_set_x(cont, 2);
    lv_cont_set_layout(cont, LV_LAYOUT_GRID);
    lv_obj_set_click(cont, false);
    lv_obj_set_style_local_bg_opa(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_0);
    lv_obj_set_style_local_pad_top(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_bottom(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_right(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_left(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_inner(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 3);
    lv_watch_obj_add_element(cont);
    lv_page_glue_obj(cont, true);

    lv_obj_t * img[ext->photo_cnt];
    lv_obj_t * img_mask[ext->photo_cnt];
    uint8_t i = 0;
    album_photo_info_t * node = NULL;
    if(ext->time_order)
        node = (album_photo_info_t *)_lv_ll_get_head(photo_ll);
    else
        node = (album_photo_info_t *)_lv_ll_get_tail(photo_ll);
    while(node) {
        i = node->index;
        img[i] = lv_img_create(cont, NULL);
        node->preview_src->header.w = 72;
        node->preview_src->header.h = 72;
        lv_img_set_src(img[i], (void *)node->preview_src);
        lv_watch_set_free_num(img[i], i);
        lv_watch_obj_add_element(img[i]);
        lv_page_glue_obj(img[i], true);
        lv_obj_set_adv_hittest(img[i], false);
        if(i == 0)
            orignal_img_signal = lv_obj_get_signal_cb(img[i]);
        if(ext->preview_mode == 0) {
            lv_obj_set_signal_cb(img[i], album_img_signal);
            lv_obj_add_protect(img[i], LV_PROTECT_PRESS_LOST);
        } else {
            img_mask[i] = lv_img_create(img[i], NULL);
            lv_img_set_src(img_mask[i], ICON_ALBUM_SELECT);
            lv_watch_set_free_num(img_mask[i], i);
            lv_watch_obj_add_element(img_mask[i]);
            lv_obj_set_click(img_mask[i], true);
            lv_page_glue_obj(img_mask[i], true);
            lv_obj_set_signal_cb(img_mask[i], album_img_signal);
            lv_obj_add_protect(img_mask[i], LV_PROTECT_PRESS_LOST);
            lv_obj_set_adv_hittest(img_mask[i], false);
        }
        if(ext->time_order)
            node = (album_photo_info_t *)_lv_ll_get_next(photo_ll, node);
        else
            node = (album_photo_info_t *)_lv_ll_get_prev(photo_ll, node);
    }
    if(ext->preview_mode == 1) {
        lv_img_set_src(ext->img_set, ICON_ALBUM_DELETE_BLUE);
        lv_obj_set_event_cb(ext->img_set, album_delete_event_cb);
    } else {
        lv_img_set_src(ext->img_set, ICON_ALBUM_MORE_SETTING);
        lv_obj_set_event_cb(ext->img_set, album_setting_event_cb);
    }
}

static void album_tabview_event_cb(lv_obj_t * tabview, lv_event_t e)
{
    if(LV_EVENT_VALUE_CHANGED == e) {
        lv_album_obj_ext_t * album_ext = album_get_ext();
        uint32_t id_cur = lv_watch_tabview_get_tab_act(tabview);
        uint32_t id_prev = *(uint32_t *) lv_event_get_data();
        album_ext->photo_tabview_curr_index += (id_cur - id_prev);
        if(album_ext->photo_cnt > PHOTO_TAB_CNT) {
             album_view_photo_tabview_create(album_ext->photo_tabview_curr_index);
         }
    }
}

static void album_view_photo_tabview_create(uint32_t img_index)
{
    lv_album_obj_ext_t * album_ext = album_get_ext();

    lv_obj_t * obj = album_get_obj();
    lv_obj_clean(obj);
    lv_obj_t  * tabview = lv_watch_tabview_create(obj);
    LV_ASSERT_MEM(tabview);
    if(tabview == NULL) return;

    lv_obj_set_size(tabview, LV_HOR_RES, LV_VER_RES);
    lv_obj_add_style(tabview, LV_TABVIEW_PART_TAB_BG, &lv_style_transp_tight);
    lv_watch_tabview_set_edge_flash(tabview, true);
    lv_obj_set_event_cb(tabview, album_tabview_event_cb);
    album_ext->tabview = tabview;

    lv_obj_t * tab;
    lv_obj_t * img_view;
    uint8_t i = 0;
    uint8_t tab_cnt = 0;
    uint8_t tab_act = 0;
    uint8_t start_index = 0;
    album_photo_info_t * node = NULL;

    if(img_index > 0) {
        start_index = img_index - 1;
        tab_act = 1;
    } else {
        start_index = img_index;
        tab_act = 0;
    }
    album_ext->photo_tabview_curr_index = img_index;
    if(album_ext->time_order)
        node = (album_photo_info_t *)_lv_ll_get_head(photo_ll);
    else
        node = (album_photo_info_t *)_lv_ll_get_tail(photo_ll);
    while(node) {
        i = node->index;
        if(tab_cnt == PHOTO_TAB_CNT) break;
        if(i >= start_index) {
            lv_img_cache_invalidate_src(node->preview_src);
            node->preview_src->header.w = 240;
            node->preview_src->header.h = 240;
            tab = lv_watch_tabview_add_tab(tabview);
            lv_obj_set_height(lv_page_get_scrl(tab), 240);
            img_view = lv_img_create(tab, NULL);
            lv_img_set_zoom(img_view, 240/node->preview_src->header.w * 256);
            lv_img_set_src(img_view, node->preview_src);
            lv_watch_set_free_num(img_view, i);
            lv_obj_set_adv_hittest(img_view, false);
            lv_obj_set_pos(lv_page_get_scrl(tab), 0, 0);
            album_view_photo_opt_create(img_view);
            tab_cnt++;
        }
        if(album_ext->time_order)
            node = (album_photo_info_t *)_lv_ll_get_next(photo_ll, node);
        else
            node = (album_photo_info_t *)_lv_ll_get_prev(photo_ll, node);
    }

    lv_watch_tabview_set_tab_act(tabview, tab_act, false);
}

static void album_view_photo_opt_create(lv_obj_t * img_view)
{
    lv_obj_t * img_back = lv_img_create(img_view, NULL);
    lv_img_set_src(img_back, ICON_ALBUM_BACK);
    if(lv_obj_get_base_dir(img_view) == LV_BIDI_DIR_RTL) {
        lv_obj_align(img_back, NULL, LV_ALIGN_IN_TOP_RIGHT, 0, 0);
        lv_img_set_angle(img_back, 1800);
    } else
        lv_obj_align(img_back, NULL, LV_ALIGN_IN_TOP_LEFT, 0, 0);
    lv_obj_set_click(img_back, true);
    lv_obj_set_adv_hittest(img_back, false);
    lv_watch_set_free_num(img_back, 4);
    lv_obj_set_event_cb(img_back, album_back_event_cb);

    lv_obj_t * img_share = lv_img_create(img_view, NULL);
    lv_img_set_src(img_share, ICON_ALBUM_SHARE);
     if(lv_obj_get_base_dir(img_view) == LV_BIDI_DIR_RTL)
        lv_obj_align(img_share, NULL, LV_ALIGN_IN_TOP_LEFT, 0, 0);
    else
        lv_obj_align(img_share, NULL, LV_ALIGN_IN_TOP_RIGHT, 0, 0);
    lv_obj_set_click(img_share, true);
    lv_obj_set_adv_hittest(img_share, false);
    lv_watch_set_free_num(img_share, lv_obj_get_user_data(img_view).user_num);
    lv_obj_set_event_cb(img_share, album_share_event_cb);

    lv_obj_t * img_del = lv_img_create(img_view, NULL);
    lv_img_set_src(img_del, ICON_ALBUM_DELETE_GRAY);
     if(lv_obj_get_base_dir(img_view) == LV_BIDI_DIR_RTL)
        lv_obj_align(img_del, NULL, LV_ALIGN_IN_BOTTOM_RIGHT, 0, 0);
    else
    lv_obj_align(img_del, NULL, LV_ALIGN_IN_BOTTOM_LEFT, 0, 0);
    lv_obj_set_click(img_del, true);
    lv_obj_set_adv_hittest(img_del, false);
    lv_obj_set_event_cb(img_del, album_opt_delete_event_cb);
}

static lv_obj_t * album_setting_create(lv_obj_t * activity_obj)
{
    lv_obj_t * bg;
    lv_obj_t * obj;

    if(NULL == activity_obj) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_ALBUM_SETTING;
        activity_ext.create = album_setting_create;
        activity_ext.prepare_destory = album_setting_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(NULL == activity_obj) {
            return NULL;
        }
    }

    obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(NULL == obj) {
        return NULL;
    }

    lv_album_obj_ext_t * ext = album_get_ext();

    bg = lv_cont_create(obj, NULL);
    lv_obj_set_size(bg, LV_HOR_RES, LV_VER_RES);
    lv_obj_set_style_local_bg_color(bg, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xB6, 0xF7, 0xFF));

    uint8_t inner_pad, inner_pad1, inner_pad2 ,inner_pad3;
    char * symbol = LV_SYMBOL_RIGHT;
    if(lv_obj_get_base_dir(obj) == LV_BIDI_DIR_RTL)
        symbol = LV_SYMBOL_LEFT;

    lv_obj_t * cont = lv_cont_create(bg, NULL);
    lv_obj_set_size(cont, LV_HOR_RES, LV_VER_RES / 7);
    lv_obj_align(cont, bg, LV_ALIGN_IN_TOP_LEFT, 0, 0);
    lv_obj_set_style_local_bg_opa(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_TRANSP);
    lv_obj_set_style_local_pad_left(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_right(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_cont_set_layout(cont, LV_LAYOUT_ROW_MID);
    lv_obj_set_click(cont, false);

    lv_obj_t * img_back = lv_img_create(cont, NULL);
    lv_img_set_src(img_back, ICON_ALBUM_BACK_BLUE);
    if(lv_obj_get_base_dir(cont) == LV_BIDI_DIR_RTL)
        lv_img_set_angle(img_back, 1800);
    lv_watch_set_free_num(img_back, 3);
    lv_obj_set_click(img_back, true);
    lv_obj_set_adv_hittest(img_back, false);
    lv_obj_set_event_cb(img_back, album_back_event_cb);

    lv_obj_t * label = lv_label_create(cont, NULL);
    lv_label_set_text_id(label, WATCH_TEXT_ID_ALBUM_SETTING);
    lv_obj_set_style_local_text_color(label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLUE);
    lv_obj_set_click(label, false);

    inner_pad = (LV_HOR_RES - lv_obj_get_width(img_back)- lv_obj_get_width(label)) / 3;
    lv_obj_set_style_local_pad_inner(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, inner_pad);

    lv_obj_t * cont1 = lv_cont_create(bg, NULL);
    lv_obj_set_size(cont1, 212, 54);
    lv_obj_set_style_local_bg_color(cont1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xB9, 0x86, 0xFF));
    lv_obj_set_style_local_radius(cont1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_DPI/15);
    lv_obj_set_style_local_pad_left(cont1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 10);
    lv_obj_set_style_local_pad_right(cont1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 10);
    lv_obj_align(cont1, cont, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
    lv_obj_set_click(cont1, true);
    lv_watch_set_free_num(cont1, 1);
    lv_obj_set_event_cb(cont1, album_setting_opt_event_cb);
    lv_cont_set_layout(cont1, LV_LAYOUT_ROW_MID);

    lv_obj_t * label_sort = lv_label_create(cont1, NULL);
    lv_label_set_text_id(label_sort, WATCH_TEXT_ID_PHOTO_ORDER);
    lv_obj_add_style(label_sort, LV_LABEL_PART_MAIN, &lv_watch_font20);

    ext->orderopt_label = lv_label_create(cont1, NULL);
    album_set_photo_order_label_text();
    lv_obj_add_style(ext->orderopt_label, LV_LABEL_PART_MAIN, &lv_watch_font20);

    lv_obj_t * label_right = lv_label_create(cont1, NULL);
    lv_label_set_text(label_right, symbol);
    lv_obj_add_style(label_right, LV_LABEL_PART_MAIN,  &lv_watch_font20);
    lv_obj_set_style_local_text_font(label_right, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_THEME_WATCH_NIGHT_SYMBOL_NORMAL);

    inner_pad1 = (lv_obj_get_width(cont1) - lv_obj_get_width(label_sort)- lv_obj_get_width(ext->orderopt_label) - lv_obj_get_width(label_right) - 20) / 2;
    lv_obj_set_style_local_pad_inner(cont1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, inner_pad1);

    lv_obj_t * cont2 = lv_cont_create(bg, cont1);
    lv_obj_set_style_local_bg_color(cont2, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xFF, 0x6F, 0x91));
    lv_obj_align(cont2, cont1, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
    lv_watch_set_free_num(cont2, 2);
    lv_obj_set_event_cb(cont2, album_setting_opt_event_cb);

    lv_obj_t * label_delmulti = lv_label_create(cont2, label_sort);
    lv_label_set_text_id(label_delmulti, WATCH_TEXT_ID_DEL_MULTI_PHOTO);

    label_right = lv_label_create(cont2, label_right);

    inner_pad2 = lv_obj_get_width(cont2) - lv_obj_get_width(label_delmulti)- lv_obj_get_width(label_right) - 20;
    lv_obj_set_style_local_pad_inner(cont2, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, inner_pad2);

    lv_obj_t * cont3 = lv_cont_create(bg, cont1);
    lv_obj_set_style_local_bg_color(cont3, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0x47, 0xA6, 0xFF));
    lv_obj_align(cont3, cont2, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
    lv_watch_set_free_num(cont3, 3);
    lv_obj_set_event_cb(cont3, album_setting_opt_event_cb);

    lv_obj_t * label_delall = lv_label_create(cont3, label_sort);
    lv_label_set_text_id(label_delall, WATCH_TEXT_ID_DEL_ALL_PHOTO);
    label_right = lv_label_create(cont3, label_right);

    inner_pad3 = lv_obj_get_width(cont3) - lv_obj_get_width(label_delall)- lv_obj_get_width(label_right) - 20;
    lv_obj_set_style_local_pad_inner(cont3, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, inner_pad3);

    return obj;
}

static void album_setting_order_create(lv_obj_t * par)
{
    lv_album_obj_ext_t * ext = album_get_ext();

    lv_obj_t * bg = lv_cont_create(par, NULL);
    lv_obj_set_size(bg, LV_HOR_RES, LV_VER_RES);
    lv_obj_set_style_local_bg_color(bg, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xB6, 0xF7, 0xFF));

    lv_obj_t * cont = lv_cont_create(bg, NULL);
    lv_obj_set_size(cont, LV_HOR_RES, LV_VER_RES / 7);
    lv_obj_align(cont, bg, LV_ALIGN_IN_TOP_LEFT, 0, 0);
    lv_obj_set_style_local_bg_opa(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_TRANSP);
    lv_obj_set_style_local_pad_left(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_right(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_cont_set_layout(cont, LV_LAYOUT_ROW_MID);
    lv_obj_set_click(cont, false);

    lv_obj_t * img_back = lv_img_create(cont, NULL);
    lv_img_set_src(img_back, ICON_ALBUM_BACK_BLUE);
    if(lv_obj_get_base_dir(cont) == LV_BIDI_DIR_RTL)
        lv_img_set_angle(img_back, 1800);
    lv_obj_align(img_back, NULL, LV_ALIGN_IN_TOP_LEFT, 0, 0);
    lv_obj_set_click(img_back, true);
    lv_obj_set_adv_hittest(img_back, false);
    lv_watch_set_free_num(img_back, 1);
    lv_obj_set_event_cb(img_back, album_back_event_cb);

    lv_obj_t * label = lv_label_create(cont, NULL);
    lv_label_set_text_id(label, WATCH_TEXT_ID_PHOTO_ORDER);
    lv_obj_align(label, NULL, LV_ALIGN_IN_TOP_MID, 0, 15);
    lv_obj_set_style_local_text_color(label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLUE);
    lv_obj_set_click(label, false);

    uint8_t inner_pad = (LV_HOR_RES - lv_obj_get_width(img_back)- lv_obj_get_width(label)) / 3;
    lv_obj_set_style_local_pad_inner(cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, inner_pad);

    lv_obj_t * cont1 = lv_cont_create(bg, NULL);
    lv_obj_set_size(cont1, 220, LV_VER_RES / 5);
    lv_obj_align(cont1, cont, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
    lv_obj_set_style_local_bg_opa(cont1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_TRANSP);
    lv_obj_set_style_local_border_color(cont1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
    lv_obj_set_style_local_border_side(cont1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_BORDER_SIDE_BOTTOM);
    lv_obj_set_style_local_border_width(cont1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 1);
    lv_cont_set_layout(cont1, LV_LAYOUT_ROW_MID);
    lv_obj_set_click(cont1, true);
    lv_obj_set_event_cb(cont1, album_setting_order_event_cb);
    lv_watch_set_free_num(cont1, 1);

    lv_obj_t * label_order = lv_label_create(cont1, NULL);
    lv_label_set_text_id(label_order, WATCH_TEXT_ID_TIME_ORDER);
    lv_obj_add_style(label_order, LV_LABEL_PART_MAIN, &lv_watch_font20_black);
    lv_obj_align(label_order, NULL, LV_ALIGN_IN_LEFT_MID, 0, 0);
    lv_obj_set_click(label_order, false);

    lv_obj_t * cont2 = lv_cont_create(bg, cont1);
    lv_obj_align(cont2, cont1, LV_ALIGN_OUT_BOTTOM_MID, 0, 0);
    lv_watch_set_free_num(cont2, 2);
    lv_obj_set_click(cont2, true);
    lv_obj_set_event_cb(cont2, album_setting_order_event_cb);
    lv_obj_t * label_reverse_order = lv_label_create(cont2, label_order);
    lv_label_set_text_id(label_reverse_order, WATCH_TEXT_ID_TIME_REVERSE_ORDER);

    lv_obj_t * img_check = lv_img_create(cont1, NULL);
    lv_img_set_src(img_check, ICON_ALBUM_CHECK);
    if(ext->time_order == 0) {
        lv_obj_set_parent(img_check, cont2);
    }

    uint8_t inner_pad1 = (lv_obj_get_width(cont1) - lv_obj_get_width(label_order)- lv_obj_get_width(img_check));
    lv_obj_set_style_local_pad_inner(cont1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, inner_pad1);

    uint8_t inner_pad2 = (lv_obj_get_width(cont2) - lv_obj_get_width(label_reverse_order)- lv_obj_get_width(img_check));
    lv_obj_set_style_local_pad_inner(cont2, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, inner_pad2);
}

static void album_setting_delete_create(lv_obj_t * par, uint8_t del_one)
{
    lv_obj_t * bg ;
    lv_obj_t * img_to_del ;
    uint8_t num = 0;

    lv_album_obj_ext_t * ext = album_get_ext();
    if(del_one) {
        bg = lv_cont_create(par, NULL);
        lv_obj_set_size(bg, LV_HOR_RES, LV_VER_RES);
        lv_obj_set_style_local_bg_color(bg, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xB6, 0xF7, 0xFF));
        img_to_del = lv_img_create(bg, NULL);
        if(ext->preview_mode == 1 && ext->selected_cnt == 1) num = 2;
        else num = 1;
    } else {
        bg = lv_img_create(par, NULL);
        lv_img_set_src(bg, ICON_ALBUM_DEL_ALL_BG);
        lv_obj_set_click(bg, true);
        img_to_del = lv_img_create(bg, NULL);
        if(ext->preview_mode == 1) num = 2;
        else num = 3;
    }
    ext->del_bg = bg;
    lv_obj_set_base_dir(bg, LV_BIDI_DIR_LTR);
    lv_obj_set_pos(img_to_del, 70, 16);
    album_photo_info_t * node = album_get_del_img_node(par, num);
    lv_img_cache_invalidate_src(node->preview_src);
    node->preview_src->header.w = 100;
    node->preview_src->header.h = 100;
    lv_img_set_src(img_to_del, node->preview_src);
    lv_watch_set_free_num(img_to_del,node->index);

    lv_obj_t * label_q = lv_label_create(bg, NULL);
    if(3 != num)
        lv_label_set_text_id(label_q, WATCH_TEXT_ID_DEL_Q);
    else
        lv_label_set_text_id(label_q, WATCH_TEXT_ID_DEL_ALL_PHOTO_Q);
    if(lv_lang_act() == LANG_CH)
        lv_obj_add_style(label_q, LV_LABEL_PART_MAIN, &lv_watch_font30_black);
    else
        lv_obj_add_style(label_q, LV_LABEL_PART_MAIN, &lv_watch_font20_black);
    lv_obj_align(label_q, img_to_del, LV_ALIGN_OUT_BOTTOM_MID, 0, 15);

    lv_obj_t * btn_cancel = lv_btn_create(bg, NULL);
    lv_obj_set_size(btn_cancel, 100, 56);
    lv_obj_set_style_local_bg_color(btn_cancel, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0x47, 0xA6, 0xFF));
    lv_obj_set_style_local_radius(btn_cancel, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 30);
    lv_obj_set_style_local_bg_color(btn_cancel, LV_BTN_PART_MAIN, LV_STATE_PRESSED, LV_COLOR_MAKE(0x47, 0xA6, 0xFF));
    lv_obj_set_style_local_radius(btn_cancel, LV_BTN_PART_MAIN, LV_STATE_PRESSED, 30);
    lv_obj_align(btn_cancel, NULL, LV_ALIGN_IN_BOTTOM_LEFT, 10, -10);
    lv_obj_set_event_cb(btn_cancel, album_cancel_btn_event_cb);
    lv_watch_set_free_num(btn_cancel, num);
    lv_obj_t * label_cancel = lv_label_create(btn_cancel, NULL);
    lv_label_set_text_id(label_cancel, WATCH_TEXT_ID_CANCEL);
    lv_obj_add_style(label_cancel, LV_LABEL_PART_MAIN, &lv_watch_font30);

    lv_obj_t * btn_ok = lv_btn_create(bg, btn_cancel);
    lv_obj_set_style_local_bg_color(btn_ok, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xFF, 0x6F, 0x91));
    lv_obj_set_style_local_radius(btn_ok, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 30);
    lv_obj_set_style_local_bg_color(btn_ok, LV_BTN_PART_MAIN, LV_STATE_PRESSED, LV_COLOR_MAKE(0xFF, 0x6F, 0x91));
    lv_obj_set_style_local_radius(btn_ok, LV_BTN_PART_MAIN, LV_STATE_PRESSED, 30);
    lv_obj_align(btn_ok, NULL, LV_ALIGN_IN_BOTTOM_RIGHT, -10, -10);
    lv_watch_set_free_num(btn_ok, num);
    lv_obj_set_event_cb(btn_ok, album_ok_btn_event_cb);
    lv_obj_t * label_ok = lv_label_create(btn_ok, label_cancel);
    lv_label_set_text_id(label_ok, WATCH_TEXT_ID_OK);
}

static void album_recreate_preview(void)
{
    lv_album_obj_ext_t * ext = album_get_ext();
    ext->timer = NULL;
    lv_obj_t * obj = album_get_obj();
    lv_obj_clean(obj);
    album_photo_info_t * node = NULL;
    _LV_LL_READ(*photo_ll, node) {
        lv_img_cache_invalidate_src(node->preview_src);
    }
    lv_obj_t * bg = album_photo_bg_create(obj);
    if(ext->preview_mode == 1) {
        ext->preview_mode = 0;
        album_reset_photo_ll_selected();
    }
    if(ext->photo_cnt > 0) {
        album_photo_item_create(bg);
    }
}

static void album_delete_success_timer_cb(void * param)
{
    if(lv_watch_get_activity_obj(ACT_ID_ALBUM) == NULL)
        return;
    album_recreate_preview();
}

static void album_delete_success_create()
{
    lv_obj_t * cont_bg = lv_cont_create(album_get_obj(), NULL);
    lv_obj_set_size(cont_bg, LV_HOR_RES, LV_VER_RES);
    lv_obj_add_style(cont_bg, LV_CONT_PART_MAIN, &lv_watch_black_tight);

    lv_obj_t * img_suc = lv_img_create(cont_bg, NULL);
    lv_img_set_src(img_suc, ICON_OK);
    lv_obj_align(img_suc, NULL, LV_ALIGN_IN_TOP_MID, 0, 30);

    lv_obj_t * label = lv_label_create(cont_bg, NULL);
    lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font30);
    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
    lv_label_set_text_id(label, WATCH_TEXT_ID_DEL_SUCCESS);
    lv_obj_align(label, img_suc, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);

    lv_album_obj_ext_t * ext = album_get_ext();
    ext->timer = Hal_Timer_Start(1000, album_delete_success_timer_cb, NULL, false);
}

static void album_photo_share_confirm_create(lv_obj_t * par, voice_msg_contact_t * contact, void * src)
{
    char str[64];

    lv_obj_t * bg = lv_cont_create(par, NULL);
    lv_obj_set_size(bg, LV_HOR_RES, LV_VER_RES);
    lv_obj_set_style_local_bg_color(bg, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xB6, 0xF7, 0xFF));

    lv_obj_t * img = lv_img_create(bg, NULL);
    lv_img_set_src(img, src);
    lv_obj_align(img, NULL, LV_ALIGN_IN_TOP_MID, 0, 10);

    lv_obj_t * label = lv_label_create(bg, NULL);
    memset(str, 0, 64);
    if(LANG_CH == lv_lang_act()) {
        snprintf(str, 64, "%s%s?", (char *)lv_lang_get_text(WATCH_TEXT_ID_SEND_TO), contact->name);
        lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font30_black);
    } else {
        snprintf(str, 64, "%s %s?", (char *)lv_lang_get_text(WATCH_TEXT_ID_SEND_TO), contact->name);
        lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font20_black);
    }
    lv_label_set_text(label, str);
    lv_label_set_long_mode(label, LV_LABEL_LONG_SROLL_CIRC);
    lv_obj_set_width(label, LV_HOR_RES - 20);
    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
    lv_obj_align(label, img, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);

    lv_obj_t * btn_cancel = lv_btn_create(bg, NULL);
    lv_obj_set_size(btn_cancel, 100, 48);
    lv_obj_set_style_local_bg_color(btn_cancel, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0x47, 0xA6, 0xFF));
    lv_obj_set_style_local_radius(btn_cancel, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 30);
    lv_obj_set_style_local_bg_color(btn_cancel, LV_BTN_PART_MAIN, LV_STATE_PRESSED, LV_COLOR_MAKE(0x47, 0xA6, 0xFF));
    lv_obj_set_style_local_radius(btn_cancel, LV_BTN_PART_MAIN, LV_STATE_PRESSED, 30);
    lv_obj_align(btn_cancel, NULL, LV_ALIGN_IN_BOTTOM_LEFT, 10, -10);
    lv_obj_set_event_cb(btn_cancel, album_share_cancel_btn_event_cb);
    lv_obj_t * label_cancel = lv_label_create(btn_cancel, NULL);
    lv_label_set_text_id(label_cancel, WATCH_TEXT_ID_CANCEL);
    lv_obj_add_style(label_cancel, LV_LABEL_PART_MAIN, &lv_watch_font30);

    lv_obj_t * btn_ok = lv_btn_create(bg, btn_cancel);
    lv_obj_set_style_local_bg_color(btn_ok, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xFF, 0x6F, 0x91));
    lv_obj_set_style_local_radius(btn_ok, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 30);
    lv_obj_set_style_local_bg_color(btn_ok, LV_BTN_PART_MAIN, LV_STATE_PRESSED, LV_COLOR_MAKE(0xFF, 0x6F, 0x91));
    lv_obj_set_style_local_radius(btn_ok, LV_BTN_PART_MAIN, LV_STATE_PRESSED, 30);
    lv_obj_align(btn_ok, NULL, LV_ALIGN_IN_BOTTOM_RIGHT, -10, -10);
    lv_watch_set_free_ptr(btn_ok, contact);
    lv_obj_set_event_cb(btn_ok, album_share_ok_btn_event_cb);
    lv_obj_t * label_ok = lv_label_create(btn_ok, label_cancel);
    lv_label_set_text_id(label_ok, WATCH_TEXT_ID_OK);
}

static void album_share_cancel_btn_event_cb(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e)
    {
        lv_obj_t * bg_obj = lv_obj_get_parent(btn);
        lv_obj_del(bg_obj);
    }
}

static void album_share_ok_btn_event_cb(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e)
    {
        lv_obj_t * album_act_obj = lv_watch_get_activity_obj(ACT_ID_ALBUM);
        lv_obj_t * camera_act_obj = lv_watch_get_activity_obj(ACT_ID_CAMERA);
        if(album_act_obj == NULL && camera_act_obj) {
            lv_obj_t * watch_obj = NULL;
            lv_camera_obj_ext_t  * ext = NULL;
            lv_watch_get_child_obj(camera_act_obj, lv_watch_obj_signal, &watch_obj);
            if(watch_obj)
                ext = lv_obj_get_ext_attr(watch_obj);
            lv_img_dsc_t * src = (lv_img_dsc_t *)lv_img_get_src(ext->img);
            voice_msg_send_photo((void *)(src->data), src->data_size, (voice_msg_contact_t *)lv_obj_get_user_data(btn).user_data);

        } else if(album_act_obj) {
            album_photo_info_t * node = NULL;
            node = (album_photo_info_t *)_lv_ll_get_head(photo_ll);
            while(node) {
                if(node->selected == 1) {
                    lv_fs_file_t file;
                    void * buf;
                    lv_fs_res_t res = lv_fs_open(&file, node->file_path, LV_FS_MODE_RD);
                    if(res == LV_FS_RES_OK) {
                        uint32_t rn;
                        buf = lv_mem_alloc(node->size);
                        memset(buf, 0, node->size);
                        lv_fs_read(&file, buf, node->size, &rn);
                        lv_fs_close(&file);
                        voice_msg_send_photo(buf, node->size, (voice_msg_contact_t *)lv_obj_get_user_data(btn).user_data);
                        lv_mem_free(buf);
                    }
                    node->selected = 0;
                    break;
                }
                node = (album_photo_info_t *)_lv_ll_get_next(photo_ll, node);
            }
        }
    }
}

static void album_ok_btn_event_cb(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e)
    {
        lv_album_obj_ext_t * ext = album_get_ext();
        uint8_t type = lv_obj_get_user_data(btn).user_num;
        lv_obj_del(lv_obj_get_parent(btn));
        ext->del_bg = NULL;
        if(lv_watch_get_activity_obj(ACT_ID_ALBUM_SETTING))
            lv_obj_del(lv_watch_get_activity_obj(ACT_ID_ALBUM_SETTING));
        lv_obj_t * obj = album_get_obj();
        lv_obj_clean(obj);
        album_photo_delete(type);
        album_delete_success_create();
    }
}

static void album_cancel_btn_event_cb(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e)
    {
        lv_album_obj_ext_t * ext = album_get_ext();
        uint8_t img_num = lv_obj_get_user_data(lv_obj_get_child_back(lv_obj_get_parent(btn), NULL)).user_num;
        uint8_t btn_num = lv_obj_get_user_data(btn).user_num;
        album_photo_info_t * node = NULL;
        node = (album_photo_info_t *)_lv_ll_get_head(photo_ll);
        while(node) {
            if(node->index == img_num) {
                lv_img_cache_invalidate_src(node->preview_src);
                if(btn_num == 1) {
                    node->preview_src->header.w = 240;
                    node->preview_src->header.h = 240;
                    node->selected = 0;
                } else {
                    node->preview_src->header.w = 72;
                    node->preview_src->header.h = 72;
                }
                break;
            }
            node = (album_photo_info_t *)_lv_ll_get_next(photo_ll, node);
        }
        lv_obj_del(lv_obj_get_parent(btn));
        ext->del_bg = NULL;
        if(ext->preview_mode == 1) {
            ext->preview_mode = 0;
            album_reset_photo_ll_selected();
            lv_obj_t * bg = lv_obj_get_parent(ext->img_page);
            lv_obj_del(ext->img_page);
            album_photo_item_create(bg);
        }
    }
}

static void album_back_event_cb(lv_obj_t * obj, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e) {
        uint8_t num = lv_obj_get_user_data(obj).user_num;
        if(num == ALBUM_BACK_ORDER_SET) {
            lv_obj_t * par_obj = lv_obj_get_parent(lv_obj_get_parent(obj));
            lv_obj_del(par_obj);
            album_set_photo_order_label_text();
        } else if(num == ALBUM_BACK_QUIT) {
            lv_album_obj_ext_t * ext = album_get_ext();
            if(ext->preview_mode == 0) {//quit album app
                album_destory(true);
                lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_ALBUM);
                if(activity_obj) lv_obj_del(activity_obj);
            } else {
                ext->preview_mode = 0;
                lv_obj_t * img = lv_obj_get_child_back(lv_obj_get_child(lv_obj_get_child(ext->img_page, NULL), NULL), NULL);
                while(img) {
                    lv_obj_del(lv_obj_get_child(img, NULL));
                    lv_obj_set_signal_cb(img, album_img_signal);
                    img = lv_obj_get_child_back(lv_obj_get_child(lv_obj_get_child(ext->img_page, NULL), NULL), img);
                }
                //lv_obj_t * img_set = lv_obj_get_child_back(lv_obj_get_parent(ext->img_page), NULL);
                lv_img_set_src(ext->img_set, ICON_ALBUM_MORE_SETTING);
                lv_obj_set_event_cb(ext->img_set, album_setting_event_cb);
            }
        } else if(num == ALBUM_BACK_SETTING) {
            lv_obj_t * activity_obj = lv_watch_get_top_activity_obj();
            if(activity_obj)
                lv_obj_del(activity_obj);
        } else if(num == ALBUM_BACK_VIEW_PHOTO) {
            album_recreate_preview();
            album_reset_photo_ll_selected();
        } else if(num == ALBUM_BACK_SEL_SHARE) {
            lv_obj_del(lv_obj_get_parent(obj));
        } else {
            lv_obj_del(lv_obj_get_child(lv_obj_get_parent(obj), NULL));
            lv_watch_set_free_num(obj, ALBUM_BACK_SEL_SHARE);
            lv_label_set_text_id(lv_obj_get_child_back(lv_obj_get_parent(obj), obj), WATCH_TEXT_ID_SHARE_TO);
        }
    }
}
static void album_setting_event_cb(lv_obj_t * obj, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e) {
        album_setting_create(NULL);
    }
}

#if USE_LV_BLUETOOTH != 0
static void album_share_to_voice_msg_action(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) return;
    lv_obj_del(lv_obj_get_parent(btn));
    voice_msg_create(NULL, 1, NULL);
}

static void album_share_select_dev_action(lv_obj_t * btn, lv_event_t e)
{
   if(LV_EVENT_CLICKED != e) return;
    char * file_path;
    lv_obj_t * album_act_obj = lv_watch_get_activity_obj(ACT_ID_ALBUM);
    lv_obj_t * camera_act_obj = lv_watch_get_activity_obj(ACT_ID_CAMERA);
    if(album_act_obj == NULL && camera_act_obj) {
        file_path = (char *)lv_obj_get_user_data(btn).user_data;
    } else if(album_act_obj) {
        album_photo_info_t * info = (album_photo_info_t *)lv_obj_get_user_data(btn).user_data;
        lv_img_cache_invalidate_src(info->preview_src);
        file_path = info->file_path;
        info->selected = 0;
        lv_album_obj_ext_t * ext = album_get_ext();
        lv_obj_del(ext->tabview);
        ext->tabview = NULL;
    }
    bluetooth_send_file_mbox(file_path);
    uint8_t num = lv_obj_get_user_data(btn).user_num;
    bt_paired_dev_t * paired_list = bt_get_paired_list();
    struct bt_addr addr;
    memcpy(addr.bytes, paired_list[num].addr, 6);
    bt_send_file_pre(file_path, addr);

    if(album_act_obj == NULL && camera_act_obj) {
        lv_obj_t * bg = lv_obj_get_parent(lv_obj_get_parent(lv_obj_get_parent(btn)));
        lv_obj_del(bg);
    }
}

static void album_share_to_bt_action(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) return;
    lv_obj_t * bg = lv_obj_get_parent(btn);
    if(bt_get_state() == false) {
        lv_obj_t * mbox = lv_msgbox_create(bg, NULL);
        lv_msgbox_set_text(mbox, (char *)lv_lang_get_text(WATCH_TEXT_ID_OPEN_BT_FIRST));
        lv_obj_set_size(mbox, LV_HOR_RES * 2 / 3, LV_VER_RES * 2 / 3);
        lv_obj_realign(mbox);
        lv_msgbox_set_anim_time(mbox, 0);
        lv_msgbox_start_auto_close(mbox, 2000);
        return;
    }
    if(bt_get_headset_connect()) {
        lv_obj_t * mbox = lv_msgbox_create(bg, NULL);
        lv_msgbox_set_text(mbox, (char *)lv_lang_get_text(WATCH_TEXT_ID_DISCNNECT_HEADSET));
        lv_obj_set_size(mbox, LV_HOR_RES * 2 / 3, LV_VER_RES * 2 / 3);
        lv_obj_realign(mbox);
        lv_msgbox_set_anim_time(mbox, 0);
        lv_msgbox_start_auto_close(mbox, 2000);
        return;
    }
    lv_obj_t * img_back = lv_obj_get_child_back(bg, NULL);
    lv_watch_set_free_num(img_back, ALBUM_BACK_SEL_DEV);
    lv_obj_t * title = lv_obj_get_child_back(bg, img_back);
    lv_label_set_text_id(title, WATCH_TEXT_ID_SELECT_DEV);

    lv_obj_t * list = lv_list_create(bg, NULL);
    lv_obj_set_size(list, LV_HOR_RES, LV_VER_RES - 40);
    lv_obj_align(list, NULL, LV_ALIGN_IN_BOTTOM_MID, 0, 0);
    lv_page_set_scrollbar_mode(list, LV_SCROLLBAR_MODE_OFF);
    lv_obj_add_style(list, LV_LIST_PART_BG, lv_obj_get_local_style(bg, LV_CONT_PART_MAIN));
    lv_obj_set_style_local_pad_inner(list, LV_LIST_PART_BG, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_left(list, LV_LIST_PART_BG, LV_STATE_DEFAULT, 5);
    lv_obj_set_style_local_pad_right(list, LV_LIST_PART_BG, LV_STATE_DEFAULT, 5);
    lv_obj_add_style(list, LV_LIST_PART_SCROLLABLE, lv_obj_get_local_style(bg, LV_CONT_PART_MAIN));
    lv_obj_set_style_local_pad_inner(list, LV_LIST_PART_SCROLLABLE, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_left(list, LV_LIST_PART_SCROLLABLE, LV_STATE_DEFAULT, 5);
    lv_obj_set_style_local_pad_right(list, LV_LIST_PART_SCROLLABLE, LV_STATE_DEFAULT, 5);

    if(bt_get_paired_dev_cnt() == 0) {
        lv_obj_t * tip_label = lv_label_create(list, NULL);
        lv_obj_add_style(tip_label, LV_LABEL_PART_MAIN, &lv_watch_font20_black);
        lv_label_set_text_id(tip_label, WATCH_TEXT_ID_NO_DEV);
        lv_obj_align(tip_label, NULL, LV_ALIGN_CENTER, 0, 0);
    } else {
        bt_paired_dev_t * paired_list = bt_get_paired_list();
        lv_obj_t * dev_btn;
        uint8_t i = 0;
        bool b_find_phone = false;
        for(i = 0; i < bt_get_paired_dev_cnt(); i++) {
            if((paired_list[i].cod & COD_TYPE_PHONE) == COD_TYPE_PHONE) {
                dev_btn = lv_list_add_btn(list, NULL, (const char *)paired_list[i].name);
                lv_watch_set_free_ptr(dev_btn, lv_obj_get_user_data(btn).user_data);
                lv_watch_set_free_num(dev_btn, i);
                lv_obj_set_event_cb(dev_btn, album_share_select_dev_action);
                lv_obj_add_style(dev_btn, LV_BTN_PART_MAIN, lv_obj_get_local_style(btn, LV_BTN_PART_MAIN));
                lv_obj_add_style(lv_obj_get_child(dev_btn, NULL), LV_LABEL_PART_MAIN, &lv_watch_font20_black);
                b_find_phone = true;
            }
        }
        if(!b_find_phone) {
            lv_obj_t * tip_label = lv_label_create(list, NULL);
            lv_obj_add_style(tip_label, LV_LABEL_PART_MAIN, &lv_watch_font20_black);
            lv_label_set_text_id(tip_label, WATCH_TEXT_ID_NO_DEV);
            lv_obj_align(tip_label, NULL, LV_ALIGN_CENTER, 0, 0);
        }
    }
}
#endif

static void album_share_event_cb(lv_obj_t * obj, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e) {
        album_photo_info_t * node = NULL;
        node = (album_photo_info_t *)_lv_ll_get_head(photo_ll);
        while(node) {
            if(node->index == lv_obj_get_user_data(obj).user_num) {
                node->selected = 1;
                break;
            }
            node = (album_photo_info_t *)_lv_ll_get_next(photo_ll, node);
        }
#if USE_LV_BLUETOOTH != 0
        album_select_share_way(lv_obj_get_parent(obj));
#else
        voice_msg_create(NULL, 1, NULL);
#endif
    }
}

static void album_opt_delete_event_cb(lv_obj_t * obj, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e) {
        lv_album_obj_ext_t * ext = album_get_ext();
        if(ext->preview_mode == 1) {
            if(ext->selected_cnt == 0)return;
            else if(ext->selected_cnt == 1)
                album_setting_delete_create(lv_obj_get_parent(obj), 1);
            else
                album_setting_delete_create(lv_obj_get_parent(obj), 0);
        } else {
            album_setting_delete_create(lv_obj_get_parent(obj), 1);
        }
    }
}

static void album_delete_event_cb(lv_obj_t * obj, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e) {
        lv_album_obj_ext_t * ext = album_get_ext();
        if(ext->preview_mode == 1) {
            if(ext->selected_cnt == 0)return;
            else if(ext->selected_cnt == 1)
                album_setting_delete_create(lv_obj_get_parent(lv_obj_get_parent(obj)), 1);
            else
                album_setting_delete_create(lv_obj_get_parent(lv_obj_get_parent(obj)), 0);
        } else {
            album_setting_delete_create(lv_obj_get_parent(lv_obj_get_parent(obj)), 1);
        }
    }
}

static lv_res_t album_img_signal(lv_obj_t * obj, lv_signal_t sign, void * param)
{
    lv_res_t res = LV_RES_OK;
    res = orignal_img_signal(obj, sign, param);
    if(res != LV_RES_OK) return res;

    lv_album_obj_ext_t * ext = album_get_ext();
    if(LV_SIGNAL_PRESSED == sign) {
        ext->pressing = true;
    } else if((true == ext->pressing) && (sign == LV_SIGNAL_RELEASED)) {
        ext->pressing = false;
        if(ext->preview_mode == 0)
            album_view_photo_tabview_create(lv_obj_get_user_data(obj).user_num);
        else {
            if(strcmp(lv_img_get_src(obj), ICON_ALBUM_SELECT) == 0) {
                album_set_photo_ll_selected(lv_obj_get_user_data(obj).user_num, 1);
                lv_img_set_src(obj, ICON_ALBUM_SELECTED);
                ext->selected_cnt++;
            } else {
                album_set_photo_ll_selected(lv_obj_get_user_data(obj).user_num, 0);
                lv_img_set_src(obj, ICON_ALBUM_SELECT);
                ext->selected_cnt--;
            }
        }
    }
    return res;
}

static lv_res_t album_item_page_scrl_signal(lv_obj_t * page_scrl, lv_signal_t sign, void * param)
{
    lv_res_t res;

    /* Include the ancient signal function */
    res = orignal_page_scrl_signal(page_scrl, sign, param);
    if(res != LV_RES_OK) return res;

    if(LV_SIGNAL_COORD_CHG == sign) {
        lv_album_obj_ext_t * ext = album_get_ext();
        if(true == ext->pressing) {
            ext->pressing = false;
        }
    }
    return LV_RES_OK;
}

static void album_setting_opt_event_cb(lv_obj_t * obj, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e) {
        lv_obj_t * par = lv_obj_get_parent(lv_obj_get_parent(obj));
        uint8_t num = lv_obj_get_user_data(obj).user_num;
        if(num == 1) {
            album_setting_order_create(par);
        } else if(num == 2) {
            lv_obj_t * activity_obj = lv_watch_get_top_activity_obj();
            lv_album_obj_ext_t * ext = album_get_ext();
            lv_obj_t * bg = lv_obj_get_parent(ext->img_page);
            if(activity_obj)
                lv_obj_del(activity_obj);
            lv_obj_del(ext->img_page);
            ext->preview_mode = 1;
            album_photo_item_create(bg);
        } else if(num == 3) {
            album_setting_delete_create(par, 0);
        }
    }
}

static void album_setting_order_event_cb(lv_obj_t * obj, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e) {
        if(lv_obj_count_children(obj) > 1) return;
        lv_obj_t * cont;
        uint8_t num = lv_obj_get_user_data(obj).user_num;
        lv_album_obj_ext_t * ext = album_get_ext();
        uint8_t temp = ext->time_order;
        if(num == 1) {
            cont = lv_obj_get_child(lv_obj_get_parent(obj), NULL);
            ext->time_order = 1;
        } else if(num == 2) {
            cont = lv_obj_get_child(lv_obj_get_parent(obj), obj);
            ext->time_order = 0;
        }
        lv_obj_t * img = lv_obj_get_child(cont, NULL);
        lv_obj_set_parent(img, obj);
        if(ext->time_order != temp) {
            lv_obj_del(ext->img_page);
            album_reset_node_index();
            album_photo_item_create(lv_obj_get_child(album_get_obj(), NULL));
        }
    }
}
#endif /*USE_LV_WATCH_ALBUM*/
